package com.lanou.moving.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.lanou.moving.R;
import com.lanou.moving.adapter.RecommendSongsAdapter;
import com.lanou.moving.constant.URL;
import com.lanou.moving.entity.RankingListDetail;
import com.lanou.moving.entity.RecommendRoot;
import com.lanou.moving.entity.RecommendSongsRoot;
import com.lanou.moving.view.MyListView;
import com.lanou.moving.volley.StringRequestUtf;
import com.lanou.moving.volley.VolleySingleton;

import java.util.ArrayList;

import it.sephiroth.android.library.picasso.Picasso;

/**
 * Created by dllo on 15/10/24.
 */
public class RecommendSongsFragment extends BackHandledFragment implements View.OnClickListener {
    private View view;
    private Context context;
    private ImageView songs_list_bg;
    private ArrayList<RecommendSongsRoot.DataEntity> entityArrayList;
    private RecommendSongsRoot songsRoot;
    private ArrayList<RankingListDetail.SongsEntity> songsArrayList;
    private RankingListDetail listDetail;
    private VolleySingleton volleySingleton;
    private RecommendSongsAdapter songsAdapter;
    private MyListView songs_list_listview;
    private ArrayList<RecommendRoot.DataEntity> dataEntityArrayList;
    private CharSequence title;
    private int position;
    private StringRequestUtf stringRequest;
    private String datas;
    private Gson gson = new Gson();

    @Override
    protected boolean onBackPressed() {
        return false;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getActivity();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.recommend_detail, null);
        volleySingleton = VolleySingleton.getInstance(context);
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.coordinator_toolbar);
        toolbar.setNavigationOnClickListener(this);
        toolbar.setTitle(title);
        toolbar.setTitleTextColor(getResources().getColor(R.color.color_white));
        songs_list_listview = (MyListView) view.findViewById(R.id.songs_list_listview);
        getDatas();
        switch (position) {
            case 0:
                getSongsList(URL.URL_LOVE);
                Picasso.with(context).load(dataEntityArrayList.get(position).getSmallPicUrls().get(0)).into(songs_list_bg);
                break;
            case 1:

                getSongsList(URL.URL_SECOND);
                Picasso.with(context).load(dataEntityArrayList.get(position).getPicUrl()).into(songs_list_bg);
                break;
            case 2:
                getSongsList(URL.URL_THIRD);
                Picasso.with(context).load(dataEntityArrayList.get(position).getPicUrl()).into(songs_list_bg);
                break;
            case 3:
                getSongsList(URL.URL_NEWSONG);
                Picasso.with(context).load(dataEntityArrayList.get(position).getSmallPicUrls().get(0)).into(songs_list_bg);
                break;
            case 4:
                getSongsList(URL.URL_FIFTH);
                Picasso.with(context).load(dataEntityArrayList.get(position).getPicUrl()).into(songs_list_bg);
                break;
            case 5:
                getSongsList(URL.URL_SIXTH);
                Picasso.with(context).load(dataEntityArrayList.get(position).getPicUrl()).into(songs_list_bg);
                break;
            case 6:
                getSongsList(URL.URL_SEVENTH);
                Picasso.with(context).load(dataEntityArrayList.get(position).getPicUrl()).into(songs_list_bg);
                break;
            case 7:
                getSongsList(URL.URL_EIGHTH);
                Picasso.with(context).load(dataEntityArrayList.get(position).getPicUrl()).into(songs_list_bg);
                break;
            case 8:
                getSongsList(URL.URL_NINETH);
                Picasso.with(context).load(dataEntityArrayList.get(position).getPicUrl()).into(songs_list_bg);
                break;
            case 9:
                getSongsList(URL.URL_TENTH);
                Picasso.with(context).load(dataEntityArrayList.get(position).getPicUrl()).into(songs_list_bg);
                break;
            case 10:
                getSongsList(URL.URL_ELEVENTH);
                Picasso.with(context).load(dataEntityArrayList.get(position).getPicUrl()).into(songs_list_bg);
                break;
            case 11:
                getSongsList(URL.URL_TWELFTH);
                Picasso.with(context).load(dataEntityArrayList.get(position).getPicUrl()).into(songs_list_bg);
                break;
        }

        return view;
    }

    public void getDatas() {
        dataEntityArrayList = (ArrayList<RecommendRoot.DataEntity>) getArguments().getSerializable("recommend");
        position = getArguments().getInt("position");
        songs_list_bg = (ImageView) view.findViewById(R.id.songs_list_bg);
        title = dataEntityArrayList.get(position).getName();
    }

    public void getSongsList(String url) {
        stringRequest = new StringRequestUtf(url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                songsRoot = gson.fromJson(response, RecommendSongsRoot.class);
                entityArrayList = (ArrayList<RecommendSongsRoot.DataEntity>) songsRoot.getData();
                songsAdapter = new RecommendSongsAdapter(context, entityArrayList);
                songs_list_listview.setAdapter(songsAdapter);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        volleySingleton.addQueue(stringRequest);
    }

    @Override
    public void onClick(View v) {
        getFragmentManager().popBackStack();
    }
}
